package com.lilei.bigdata.mr.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Mapper;


import java.io.IOException;

/**
 * keyin mr框架读取起始的偏移量
 * valuein 输入的一行文本内容
 *
 *  KEYOUT：是用户自定义逻辑处理完成之后输出数据中的key，在此处是单词，String，同上，用Text
 *  Valueout 单词出现的次数
 */
public class WordCountMapper extends Mapper<LongWritable, Text,Text, IntWritable> {
    /**
     *
     * @param key   每一行的偏移量
     * @param value 每一行的内容
     * @param context 上下文
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] words= line.split(" ");
        for (String word:words){
            //将单词作为Key value为1  根据单词分发 相同的单词会分发到同一个reducetask
            context.write(new Text(word),new IntWritable(1));
        }
    }
}
